package com.orange.modules.dao.manage;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.orange.dbhelp.DbHelper;
import com.orange.modules.entity.manage.AuthSubject;

@Repository
public class AuthSubjectRepository {

	@Resource(name = "dbHelper")
	private DbHelper dbHelper;
	
	@Autowired
	private AuthSubjectDao authSubjectDao;
	
	/**
	 * 保存学科
	 */
	public void saveAuthSubject(AuthSubject as){
		authSubjectDao.save(as);
	}
	
	/**
	 * 查询学科
	 * @throws SQLException 
	 */
	public List<AuthSubject> queryAuthSubjectByTaskId(String taskId) throws SQLException{
		String sql = "select a.p_id as pid, a.s_name as name, a.s_task_id as taskId, a.s_state as state from t_auth_subject as a" +
				" where 1=1 and a.s_task_id = ?";
		return dbHelper.getBeanList(sql, AuthSubject.class, taskId);
	}
	
	/**
	 * 查询所有的学科
	 * @throws SQLException 
	 */
	public List<AuthSubject> queryAllAuthSubject() throws SQLException{
		String sql ="select a.p_id as pid, a.s_name as name, a.s_task_id as taskId, a.s_state as state from t_auth_subject as a where 1=1";
		return dbHelper.getBeanList(sql, AuthSubject.class);
		
	}
	
	/**
	 * 根据pid查询
	 */
	public AuthSubject queryAuthSubjectByPId(String pid) throws SQLException{
		String sql = "select a.p_id as pid, a.s_name as name, a.s_task_id as taskId, a.s_state as state from t_auth_subject as a" +
				" where 1=1 and a.p_id = ?";
		return dbHelper.getBean(sql, AuthSubject.class, pid);
	}
	
	/**
	 * 查询各科目题库是否为空
	 */
	public List<AuthSubject> queryAuthSubjectByExam(String taskId) throws SQLException{
		String sql ="select DISTINCT a.p_id as pid, a.s_name as name, a.s_task_id as taskId, a.s_state as state from t_auth_subject as a left join t_auth_exam as b on a.p_id = b.s_subject" +
				" where 1=1 and a.s_task_id = ?";
		return dbHelper.getBeanList(sql, AuthSubject.class,taskId);
	}
	
	/**
	 * 修改状态
	 */
	public void updataAuthSubject(AuthSubject as){
		authSubjectDao.save(as);
	}
	
	/**
	 * 删除学科
	 */
	public void deleteAuthSubject(String pid){
		authSubjectDao.delete(pid);
	}
}
